﻿/*********************************************************************************
**                                                                              **
**  Copyright (C) 2022-2022 LiLong                                              **
**  This file is part of BluetoothCpp.                                          **
**                                                                              **
**  BluetoothCpp is free software: you can redistribute it and/or modify        **
**  it under the terms of the GNU Lesser General Public License as published by **
**  the Free Software Foundation, either version 3 of the License, or           **
**  (at your option) any later version.                                         **
**                                                                              **
**  BluetoothCpp is distributed in the hope that it will be useful,             **
**  but WITHOUT ANY WARRANTY; without even the implied warranty of              **
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               **
**  GNU Lesser General Public License for more details.                         **
**                                                                              **
**  You should have received a copy of the GNU Lesser General Public License    **
**  along with BluetoothCpp.  If not, see <https://www.gnu.org/licenses/>.      **
**********************************************************************************/
#include <BluetoothCpp/Authentication.h>
#include <BluetoothCpp/Device.h>
#include <BluetoothCpp/Instance.h>

#include <condition_variable>
#include <iostream>
#include <mutex>

int main(int argc, char* argv[])
{
    BTH::Instance inst;
    BTH::Authentication au;
    std::mutex mu;
    std::condition_variable cv;

    std::cout << "Is Connectable: " << std::boolalpha << au.isConnectable() << std::endl;
    std::cout << "Is Discoverable: " << std::boolalpha << au.isDiscoverable() << std::endl;

    std::cout << "Input \"stop\" to exit." << std::endl;
    bool accept { false };
    bool archived { false };
    bool ready { false };
    au.listen(
        [&](const auto& device, auto pin) -> bool
        {
            std::cout << "Name:                 " << device.name() << std::endl;
            std::cout << "Address:              " << device.address().toString() << std::endl;
            std::cout << "Connected:            " << std::boolalpha << device.isConnected() << std::endl;
            std::cout << "Authenticated:        " << std::boolalpha << device.isAuthenticated() << std::endl;
            std::cout << "Remembered:           " << std::boolalpha << device.isRemembered() << std::endl;
            std::cout << "Last Seen:            " << device.lastSeenTime() << std::endl;
            std::cout << "Last Used:            " << device.lastUsedTime() << std::endl;
            std::cout << "Major Device Class:   " << device.majorDeviceClass() << std::endl;
            std::cout << "PIN:" << pin << std::endl;
            std::cout << "Accept Y/N?" << std::endl;
            archived = true;
            std::unique_lock lock(mu);
            cv.wait(lock, [&]() { return ready; });
            ready    = false;
            archived = false;
            return accept;
        });
    for (;;)
    {
        std::string input;
        std::cin >> input;
        if (input == "stop")
            return 0;
        if (archived)
        {
            std::unique_lock<std::mutex> lock(mu);
            accept = input == "Y";
            ready  = true;
            lock.unlock();
            cv.notify_one();
        }
    }
}
